﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Service;
using MODEL;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Command;
using System.Windows.Input;
using Microsoft.Practices.ServiceLocation;
using GalaSoft.MvvmLight.Views;
using ViewModel.MessageRelease;
using GalaSoft.MvvmLight.Ioc;
using Common;


namespace ViewModel.AlertCreate
{
    public class AlertRecordViewModel: ViewModelBase
    {
        private IAlertRecordService m_AlertRecordService;

        private IStationServcie m_StationService;

        public ICommand ShowDetailCommand{ get; set; }

        public ICommand ResolveAlertCommand { get; set; }

        private List<AlertRecord> m_AlertRecords { get; set; }
        public List<AlertRecord> AlertRecords
        {
            get { return m_AlertRecords; }
            set { this.m_AlertRecords = value; base.RaisePropertyChanged(() => AlertRecords); }
        }

        public AlertRecordViewModel(IAlertRecordService alertMakeRecordservice, IStationServcie stationServcie)
        {           
            this.m_AlertRecordService = alertMakeRecordservice;
            this.m_StationService = stationServcie;
            this.ShowDetailCommand = new RelayCommand<AlertRecord>(ShowDetail);
            this.ResolveAlertCommand = new RelayCommand<AlertRecord>(ShowMessageReleaseForm);
            RefreshData();
        }

        public async void ShowDetail(AlertRecord alertRecorder)
        {
            AlertRecordDetailViewModel vm = new AlertRecordDetailViewModel() { ParentViewModel = this,AlertRecord = alertRecorder };
           ;
            if (string.IsNullOrEmpty(alertRecorder.RealDataId))
            {
                vm.StationData = await m_StationService.GetStationCountDataByIdAsync(alertRecorder.CountDataId);              
            }
            else
            {
                vm.StationData = await m_StationService.GetStationRealDataByIdAsync(alertRecorder.RealDataId);
            }
           this.MessengerInstance.Send<ViewModelBase>(vm, "AlertRecord_ShowDetail");
        }

        public async void ShowMessageReleaseForm(AlertRecord alertRecord)
        {           
            StationData stationData = null;
            try
            {
                if (string.IsNullOrEmpty(alertRecord.RealDataId))
                {
                    stationData = await m_StationService.GetStationCountDataByIdAsync(alertRecord.CountDataId);
                }
                else
                {
                    stationData = await m_StationService.GetStationRealDataByIdAsync(alertRecord.RealDataId);
                }
            }
            catch (Exception ex)
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowError($"查询当前数据失败:{ex.Message}", "错误", "确定", null);
                return;
            }

            string insertTime = alertRecord.InsertTime.HasValue ? alertRecord.InsertTime.Value.ToString("yyyy-MM-dd tt:mm:ss") : string.Empty;
            string minValue = alertRecord.AlertMakeInfo.MinVal.HasValue ? alertRecord.AlertMakeInfo.MinVal.ToString() : string.Empty;
            string maxValue = alertRecord.AlertMakeInfo.MaxVal.HasValue ? alertRecord.AlertMakeInfo.MaxVal.Value.ToString() : string.Empty;
            string realValue = stationData.ElementVal.HasValue ? stationData.ElementVal.Value.ToString() : string.Empty;

            MessageReleaseViewModel vm = SimpleIoc.Default.GetInstanceWithoutCaching<MessageReleaseViewModel>();

            vm.ReleaseType = MessageReleaseType.UserAlert;
            vm.MessageInfo = new MessageInfo
            {
                Title = $"【注意】{alertRecord.AlertMakeInfo.StationInfo.StationName}:{alertRecord.AlertMakeInfo.ElementInfo.ElementName}【异常】",
                Content = $"时间:{insertTime + Environment.NewLine}站点:{alertRecord.AlertMakeInfo.StationInfo.StationName + Environment.NewLine}正常:{minValue}-{maxValue + Environment.NewLine}当前:{realValue}",
                AlertMakeRecord = alertRecord
            };
          
            this.MessengerInstance.Send<ViewModelBase>(vm, "AlertRecord_ShowMessageReleaseForm");                     
        }

        public async void RefreshData(Action AfterRefreshAction = null)
        {
            try
            {
                if (AppSetting.Instance.User.role.IsAdmin)
                {
                    this.AlertRecords = (await m_AlertRecordService.GetAlertRecordsAsync()).ToList();
                }
                else
                {
                    this.AlertRecords = (await m_AlertRecordService.GetAlertRecordsAsync(AppSetting.Instance.User.UserId)).ToList();
                }
            }
            catch (Exception ex)
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowError("加载数据失败", "错误", "确定", null);
                Log.WriteException(ex);   
            }
        }
    }
}
